From 8cb86a726b74fe1079ef8d405fe7943c559cb0ff Mon Sep 17 00:00:00 2001 From: robertl Date: Mon, 27 Jul 2009 15:36:30 +0000 Subject: [PATCH] Sketch in 'update' option for GUI. --- gui/app.pro | 6 ++- gui/upgrade.cpp | 134 ++++++++++++++++++++++++++++++++++++++++++++++++ gui/upgrade.h | 66 ++++++++++++++++++++++++ gui/upgrade.ui | 78 ++++++++++++++++++++++++++++ 4 files changed, 282 insertions(+), 2 deletions(-) create mode 100644 gui/upgrade.cpp create mode 100644 gui/upgrade.h create mode 100644 gui/upgrade.ui diff --git a/gui/app.pro b/gui/app.pro index 194fb6bd6..a78f585d6 100755 --- a/gui/app.pro +++ b/gui/app.pro @@ -1,4 +1,4 @@ -# $Id: app.pro,v 1.4 2009/07/23 03:22:23 robertl Exp $ +# $Id: app.pro,v 1.5 2009/07/27 15:36:30 robertl Exp $ # #CONFIG += qt debug console @@ -40,6 +40,7 @@ FORMS += wayptsui.ui FORMS += rttrkui.ui FORMS += miscfltui.ui FORMS += gmapui.ui +FORMS += upgrade.ui SOURCES += advdlg.cpp SOURCES += dpencode.cpp @@ -58,7 +59,7 @@ SOURCES += optionsdlg.cpp SOURCES += processwait.cpp SOURCES += filterwidgets.cpp SOURCES += filterdlg.cpp - +SOURCES += upgrade.cpp HEADERS += mainwindow.h HEADERS += map.h @@ -77,6 +78,7 @@ HEADERS += processwait.h HEADERS += filterwidgets.h HEADERS += filterdata.h HEADERS += setting.h +HEADERS += upgrade.h TRANSLATIONS += gpsbabelfe_de.ts TRANSLATIONS += gpsbabelfe_es.ts diff --git a/gui/upgrade.cpp b/gui/upgrade.cpp new file mode 100644 index 000000000..4ba06e630 --- /dev/null +++ b/gui/upgrade.cpp @@ -0,0 +1,134 @@ +/* + Copyright (C) 2009 Robert Lipe, robertlipe@gpsbabel.org + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA + + */ + + +#include "upgrade.h" +#include "mainwindow.h" +#include "ui_upgrade.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +Upgrade::Upgrade(QWidget *parent) : + QDialog(parent), + m_ui(new Ui::Upgrade) +{ + m_ui->setupUi(this); +} + +Upgrade::~Upgrade() +{ + delete m_ui; +} + +void Upgrade::changeEvent(QEvent *e) +{ + QDialog::changeEvent(e); + switch (e->type()) { + case QEvent::LanguageChange: + m_ui->retranslateUi(this); + break; + default: + break; + } +} + +Upgrade::updateStatus Upgrade::checkForUpgrade() +{ + http = new QHttp; + + connect(http, SIGNAL(requestFinished(int, bool)), + this, SLOT(httpRequestFinished(int, bool))); + connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), + this, SLOT(readResponseHeader(const QHttpResponseHeader &))); + + http->setHost("www.gpsbabel.org"); + httpRequestId = http->get("/updates.xml"); + + return Upgrade::updateUnknown; +} + +void Upgrade::readResponseHeader(const QHttpResponseHeader &responseHeader) +{ + switch (responseHeader.statusCode()) { + case 200: // Ok + case 301: // Moved Permanently + case 302: // Found + case 303: // See Other + case 307: // Temporary Redirect + // these are not error conditions + break; + + default: + QMessageBox::information(this, tr("HTTP"), + tr("Download failed: %1.") + .arg(responseHeader.reasonPhrase())); + httpRequestAborted = true; + http->abort(); + } +} + +void Upgrade::httpRequestFinished(int requestId, bool error) +{ + if(error or requestId != httpRequestId) { + return; + } + + QString oresponse(http->readAll()); + QDomDocument document; + if (!document.setContent(oresponse)) { + return; + } + + QString response("snore"); + + QString currentVersion = "1.3.6"; // FIXME: Work with Khai to pry this out of MainWindow + bool allowBeta = false; // TODO: come from prefs or current version... + + QDomNodeList upgrades = document.elementsByTagName("update"); + + for (unsigned int i = 0; i < upgrades.length(); i++) { + QDomNode upgradeNode = upgrades.item(i); + QDomElement upgrade = upgradeNode.toElement(); + + QString updateVersion = upgrade.attribute("version"); + bool updateIsBeta = upgrade.attribute("type") == "beta"; + bool updateIsMajor = upgrade.attribute("type") == "major"; + bool updateCandidate = updateIsMajor || (updateIsBeta && allowBeta); + + if(updateVersion > currentVersion && updateCandidate) { + response = "A new version of GPSBabel is available
"; + response += "Your version is " + currentVersion + "
";; + response += "The latest version is " + updateVersion + "
";; + break; + } + } + + // FIXME: this doesn't actually write into the UI's text browser... + m_ui->textBrowser->setText(response); + + //QMessageBox::information(this, tr("Finished"), response); + +} diff --git a/gui/upgrade.h b/gui/upgrade.h new file mode 100644 index 000000000..7754c7e54 --- /dev/null +++ b/gui/upgrade.h @@ -0,0 +1,66 @@ +#ifndef UPGRADE_H +#define UPGRADE_H +/* + Copyright (C) 2009 Robert Lipe, robertlipe@gpsbabel.org + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA + + */ + + +#include +#include "ui_upgrade.h" + +class QHttp; +class QHttpResponseHeader; + +namespace Ui { + class Upgrade; +} + +class Upgrade : public QDialog { + Q_OBJECT +public: + Upgrade(QWidget *parent = 0); + ~Upgrade(); + + typedef enum { + updateUnknown, + updateCurrent, + updateNeeded, + } updateStatus; + + Upgrade::updateStatus checkForUpgrade(void); + +protected: + void changeEvent(QEvent *e); + +private: +// Ui::Upgrade *m_ui; + Ui_Upgrade *m_ui; + QHttp *http; + int httpRequestId; + bool httpRequestAborted; + QString latestVersion; + +private slots: + void httpRequestFinished(int requestId, bool error); +// void httpStateChanged(int state); + void readResponseHeader(const QHttpResponseHeader &responseHeader); + + +}; + +#endif // UPGRADE_H diff --git a/gui/upgrade.ui b/gui/upgrade.ui new file mode 100644 index 000000000..7242ca70a --- /dev/null +++ b/gui/upgrade.ui @@ -0,0 +1,78 @@ + + + Upgrade + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + 30 + 240 + 341 + 32 + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + 10 + 10 + 381 + 211 + + + + + + + + buttonBox + accepted() + Upgrade + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + Upgrade + reject() + + + 316 + 260 + + + 286 + 274 + + + + + -- 2.30.2